package com.qf.spring003.utils;

import org.springframework.beans.factory.FactoryBean;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class MyProxyFactoryBean implements FactoryBean<Object> {
    // 原始的业务对象
    private Object target;

    public Object getTarget() {
        return target;
    }

    public void setTarget(Object target) {
        this.target = target;
    }

    @Override
    public boolean isSingleton() {
        return true;
    }

    @Override
    public Object getObject() throws Exception {
        Object o = Proxy.newProxyInstance(this.target.getClass().getClassLoader(), target.getClass().getInterfaces()
                , new InvocationHandler() {
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        final long start = System.currentTimeMillis();

                        final Object result = method.invoke(target, args);

                        final long end = System.currentTimeMillis();

                        System.out.println("time used ===>" + (end - start));

                        return result;
                    }
                });
        return o;
    }

    @Override
    public Class<?> getObjectType() {
        return target.getClass();
    }
}
